package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.imaster.dto.ShopWindowDTO;
import com.eastfair.imaster.dto.ShopWindowPageQuery;
import com.eastfair.imaster.dto.ShopWindowSaveDTO;
import com.eastfair.imaster.dto.ShopWindowUpdateDTO;
import com.eastfair.imaster.entity.ShopWindow;
import com.eastfair.imaster.service.ShopWindowService;
import com.eastfair.imaster.vo.ShopWindowVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 电子橱窗表
 * </p>
 *
 * @author zjn
 * @date 2022-03-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopWindow")
@Api(value = "ShopWindow", tags = "电子橱窗表")
public class ShopWindowController extends SuperController<ShopWindowService, Long, ShopWindow, ShopWindowPageQuery, ShopWindowSaveDTO, ShopWindowUpdateDTO> {

    @Autowired
    private ShopWindowService shopWindowService;

    @Override
    public void handlerResult(IPage<ShopWindow> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ShopWindow> shopWindowList = list.stream().map((map) -> {
            ShopWindow shopWindow = ShopWindow.builder().build();
            //TODO 请在这里完成转换
            return shopWindow;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopWindowList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ShopWindowPageQuery> params) {
        ShopWindowPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 新增电子橱窗
     *
     * @param shopWindowDTO 电子橱窗
     * @return 是否成功
     */
    @ApiOperation(value = "新增电子橱窗", notes = "新增电子橱窗")
    @PostMapping("/saveShopWindow")
    public R<Boolean> saveShopWindow(@RequestBody ShopWindowDTO shopWindowDTO) {
        return shopWindowService.saveShopWindow(shopWindowDTO);
    }

    /**
     * 删除电子橱窗
     *
     * @param id 电子橱窗id
     * @return 是否成功
     */
    @ApiOperation(value = "删除电子橱窗", notes = "删除电子橱窗")
    @DeleteMapping("/deleteShopWindow/{id}")
    public R<Boolean> deleteShopWindow(@PathVariable("id") Long id) {
        return shopWindowService.deleteShopWindow(id);
    }

    /**
     * 修改电子橱窗
     *
     * @param shopWindowDTO 电子橱窗
     * @return 是否成功
     */
    @ApiOperation(value = "修改电子橱窗", notes = "修改电子橱窗")
    @PutMapping("/updateShopWindow")
    public R<Boolean> updateShopWindow(@RequestBody ShopWindowDTO shopWindowDTO) {
        return shopWindowService.updateShopWindow(shopWindowDTO);
    }

    /**
     * 通过id获取电子橱窗详情
     *
     * @param id 主键id
     * @return 电子橱窗实体
     */
    @ApiOperation(value = "修改电子橱窗", notes = "修改电子橱窗")
    @GetMapping("/getShopWindowById/{id}")
    public R<ShopWindowVO> getShopWindowById(@PathVariable("id") Long id) {
        return shopWindowService.getShopWindowById(id);
    }

    /**
     * 电子橱窗列表
     *
     * @param params 电子橱窗列表
     * @return 电子橱窗实体
     */
    @ApiOperation(value = "电子橱窗列表", notes = "电子橱窗列表")
    @PostMapping("/pageShopWindow")
    public R<Page<ShopWindowVO>> pageShopWindow(@RequestBody PageParams<ShopWindowDTO> params) {
        return shopWindowService.pageShopWindow(params);
    }

    /**
     * 简单修改
     * 不修改点位信息
     *
     * @param shopWindowDTO 电子橱窗
     * @return 是否成功
     */
    @ApiOperation(value = "简单修改", notes = "简单修改")
    @PutMapping("/simpleUpdate")
    public R<Boolean> simpleUpdate(@RequestBody ShopWindowDTO shopWindowDTO) {
        return shopWindowService.simpleUpdate(shopWindowDTO);
    }

    /**
     * 推荐橱窗列表
     *
     * @return 推荐橱窗列表
     */
    @ApiOperation(value = "推荐橱窗列表", notes = "推荐橱窗列表")
    @GetMapping("/listRecommend")
    public R<List<ShopWindowVO>> listRecommend() {
        return shopWindowService.listRecommend();
    }

}
